﻿import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
import kliment.geom.SimplePoint;

import kliment.geom.draw.FillColorStyle;
import kliment.geom.draw.FillBitmapStyle;
import kliment.geom.draw.GradientStyle;
import kliment.geom.draw.LineStyle;
import kliment.geom.draw.DrawStyle;
import kliment.geom.Geom;
/**
 * ...
 * @author	Kliment
 * @version	2.5.3
 */

class kliment.geom.draw.Draw {
	static public function lines(target_mc:MovieClip, points:Array,	drawStyle:DrawStyle):MovieClip {
		if (!drawStyle) drawStyle = new DrawStyle();
		_setStyle(target_mc, drawStyle);
		var currentPoint:SimplePoint = points[0];
		target_mc.moveTo(currentPoint.x, currentPoint.y);
		var length:Number = points.length;
		for (var i:Number = 1; i < length; ++i){
			currentPoint = points[i];
			target_mc.lineTo(currentPoint.x, currentPoint.y);
		}
		target_mc.endFill();
		return target_mc;
	}
	
	static public function rectangle(target_mc:MovieClip, size:Rectangle, drawStyle:DrawStyle):MovieClip {
		if (!drawStyle) drawStyle = new DrawStyle();
		_setStyle(target_mc, drawStyle);
		var width:Number = size.x + size.width;
		var height:Number = size.y + size.height;
		target_mc.moveTo(size.x, size.y);
		target_mc.lineTo(width, size.y);
		target_mc.lineTo(width, height);
		target_mc.lineTo(size.x, height);
		target_mc.lineTo(size.x, size.y);
		target_mc.endFill();
		return target_mc;
	}
	
	static public function roundedRectangle(target_mc:MovieClip, size:Rectangle, cornerRadius:Number, drawStyle:DrawStyle):MovieClip {
		if (!drawStyle) drawStyle = new DrawStyle();
		_setStyle(target_mc, drawStyle);
		var width:Number = size.x + size.width;
		var height:Number = size.y + size.height;
		var cX:Number = cornerRadius + size.x;
		var cY:Number = cornerRadius + size.y;
		var cWidth:Number = width - cornerRadius;
		var cHeight:Number = height - cornerRadius;
		target_mc.moveTo(cX, size.y);
		target_mc.lineTo(cWidth, size.y);
		target_mc.curveTo(width, size.y, width, cY);
		target_mc.lineTo(width, cHeight);
		target_mc.curveTo(width, height, cWidth, height);
		target_mc.lineTo(cX, height);
		target_mc.curveTo(size.x, height, size.x, cHeight);
		target_mc.lineTo(size.x, cY);
		target_mc.curveTo(size.x, size.y, cX, size.y);
		target_mc.lineTo(cX, size.y);
		target_mc.endFill();
		return target_mc;
	}
	
	static public function circle(target_mc:MovieClip, radius:Number, center:Point, drawStyle:DrawStyle):MovieClip {
		if (!drawStyle) drawStyle = new DrawStyle();
		_setStyle(target_mc, drawStyle);
		var x:Number = radius;
		var y:Number = radius;
		var tan:Number = Math.tan(Math.PI * .125) * radius;
		var sin:Number = Math.sin(Math.PI * .25) * radius;
		target_mc.moveTo(x + radius, y);
		target_mc.curveTo(radius + x, tan + y, sin + x, sin + y);
		target_mc.curveTo(tan + x, radius + y, x, radius + y);
		target_mc.curveTo(-tan + x, radius+ y, -sin + x, sin + y);
		target_mc.curveTo(-radius + x, tan + y, -radius + x, y);
		target_mc.curveTo(-radius + x, -tan + y, -sin + x, -sin + y);
		target_mc.curveTo(-tan + x, -radius + y, x, -radius + y);
		target_mc.curveTo(tan + x, -radius + y, sin + x, -sin + y);
		target_mc.curveTo(radius + x, -tan + y, radius + x, y);
		target_mc.endFill();
		return target_mc;
	}
	
	static public function colorBar(target_mc:MovieClip, size:Rectangle):MovieClip {
		var width:Number = size.x + size.width;
		var height:Number = size.y + size.height;
		
		var colorPercent:Number;
		var radians:Number;
		var r:Number;
		var g:Number;
		var b:Number;
		var color:Number;
		var matrixW:Matrix;
		var matrixB:Matrix;
		var halfHeight:Number;
		
		target_mc.clear()
		
		halfHeight = size.height * 0.5;
		
		matrixW = new Matrix();
		matrixW.createGradientBox(1,  halfHeight, Math.PI * 0.5, size.x, size.y);
		matrixB = new Matrix();
		matrixB.createGradientBox(1,  halfHeight, Math.PI * 0.5, size.x, size.y +  halfHeight);
		
		for(var i:Number = 0; i < size.width; i++)	{
			colorPercent = i / size.width;
			radians = Geom.gradToRad(-360 * colorPercent)
			
			r = Math.cos(radians) * 127 + 128 << 16;
			g = Math.cos(radians + 2 * Math.PI / 3) * 127 + 128 << 8;
			b = Math.cos(radians + 4 * Math.PI / 3) * 127 + 128;
			
			color  = r | g | b;
			
			target_mc.lineStyle(1, 0, 1, false, LineStyle.SCALE_NONE, LineStyle.CAPSSTYLE_NONE);
			target_mc.lineGradientStyle(GradientStyle.TYPE_LINEAR, [0xffffff, color], [100, 100], [0, 255], matrixW);
			target_mc.moveTo(size.x + i, size.y);
			target_mc.lineTo(size.x + i, size.y + halfHeight);
			target_mc.lineGradientStyle(GradientStyle.TYPE_LINEAR, [color, 0], [100, 100], [0, 255], matrixB);
			target_mc.moveTo(size.x + i, size.y + halfHeight);
			target_mc.lineTo(size.x + i, height);
		}
		target_mc.endFill();
		return target_mc;
	}
	
	static public function colorWheel( target_mc:MovieClip, center:Point, radius:Number):MovieClip {
		var radians:Number;
		var r:Number;
		var g:Number;
		var b:Number;
		var color:Number;
		var matrix:Matrix;
		var x:Number;
		var y:Number;
		var thickness:Number;
		
		target_mc.clear();
		
		thickness = 1 + Math.round(radius / 50);
		
		for(var i:Number = 0; i < 360; i++)	{
			radians = Geom.gradToRad(i);
			
			r = Math.cos(radians) * 127 + 128 << 16;
			g = Math.cos(radians + 2 * Math.PI / 3) * 127 + 128 << 8;
			b = Math.cos(radians + 4 * Math.PI / 3) * 127 + 128;
			
			color = r | g | b;
			
			x = center.x + radius * Math.cos(radians);
			y = center.y + radius * Math.sin(radians);
			
			matrix = new Matrix();
			matrix.createGradientBox(radius * 2, radius * 2, radians, -radius + center.x, -radius + center.y);
			
			target_mc.lineStyle(thickness, 0, 1, false, LineStyle.SCALE_NONE, LineStyle.CAPSSTYLE_NONE);
			target_mc.lineGradientStyle(GradientStyle.TYPE_LINEAR, [0xffffff, color], [100, 100], [127, 255], matrix);
			target_mc.moveTo(center.x, center.y);
			target_mc.lineTo(x, y);
		}
		target_mc.endFill();
		return target_mc;
	}
	
	static private function _setStyle(target_mc:MovieClip, drawStyle:DrawStyle) {
		if (drawStyle.fillBitmap) 
			target_mc.beginBitmapFill(drawStyle.fillBitmap.bmp, drawStyle.fillBitmap.matrix, drawStyle.fillBitmap.repeat, drawStyle.fillBitmap.smoothing);
		else if (drawStyle.fillGradientStyle) 
			target_mc.beginGradientFill(drawStyle.fillGradientStyle.fillType, drawStyle.fillGradientStyle.colors, drawStyle.fillGradientStyle.alphas, drawStyle.fillGradientStyle.ratios, drawStyle.fillGradientStyle.matrix, drawStyle.fillGradientStyle.spreadMethod, drawStyle.fillGradientStyle.interpolationMethod, drawStyle.fillGradientStyle.focalPointRatio)
		else if (drawStyle.fillColorStyle) 
			target_mc.beginFill(drawStyle.fillColorStyle.color, drawStyle.fillColorStyle.alpha);
		else 
			target_mc.beginFill(0x000000, 100);
			
		if (drawStyle.lineStyle) 
			target_mc.lineStyle(drawStyle.lineStyle.thickness, drawStyle.lineStyle.color, drawStyle.lineStyle.alpha, drawStyle.lineStyle.pixelHinting, drawStyle.lineStyle.noScale, drawStyle.lineStyle.capsStyle, drawStyle.lineStyle.jointStyle, drawStyle.lineStyle.miterLimit);
		if (drawStyle.lineGradientStyle) 
			target_mc.lineGradientStyle(drawStyle.lineGradientStyle.fillType, drawStyle.lineGradientStyle.colors, drawStyle.lineGradientStyle.alphas, drawStyle.lineGradientStyle.ratios, drawStyle.lineGradientStyle.matrix, drawStyle.lineGradientStyle.spreadMethod, drawStyle.lineGradientStyle.interpolationMethod, drawStyle.lineGradientStyle.focalPointRatio)
	}
}